package com.person.leetcode.editor.cn;

import java.util.List;

/**
  * 206.反转链表
  * 2022-08-23 09:39:38
**/
public class Sequence_0206_ReverseLinkedList {
    public static void main(String[] args) {
        Solution solution = new Sequence_0206_ReverseLinkedList().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode root) {
        ListNode tail = root;
        if (root == null || root.next == null){
            return root;
        }else{
            ListNode head = root.next;
            while(head != null){
                ListNode t = head;
                head = head.next;
                t.next = tail;
                if (tail.next == t){
                    tail.next=null;
                }
                tail = t;
            }
        }
        return tail;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}